I am using a LayzVStack embedded into a ScrollView. The list items are fetched from a core data store by using a @FetchResult or I tested it also with the new @Query command coming with SwiftData.
The list has one hundred items 1, 2, 3, ..., 100.
The user scrolled the ScrollView so that items 50, 51, ... 60 are visible on screen.
Now new data will be fetched from the server and updates the CoreData or SwiftData model. When I add new items to the end of the list (e.g 101, 102, 103, ...) then the ScrollView is keeping its position.
Opposite to this when I add new items to the top (0, -1, -2, -3, ...) then the ScrollView scrolls down.
Is there a way with the new SwiftData and SwiftUI ScrollView modifiers to update my list model without scrolling like with UIKit where you can query and set the scroll offset pixel wise?
Post
Replies
Boosts
Views
Activity
Based on the session video 'Loading and Displaying a Large Data Feed' I observed a performance problem when the number of displayed rows in a List or LazyVStack increases.
Instead of an earthquake app lets think of a messaging app. New messages are fetched from a server, injest in a background task into a core data persistent store and displayed with a SwiftUI LazyVStack embedded in a scrollview.
The rows in the list have different height. But this is not the problem because I wrote also a test app with a fixed content height for each row.
Scrolling through a list of data retrieved by a @FetchRequest works pretty well. I passed the fetched items using a @ObservedObject to the SwiftUI row views so that property faults are only handled for the displayed rows. Row views have also an identifier set so that SwiftUI can reuse them.
Everything works fine like in the earthquake example till to the point where the number of fetched items increases (approximate >2000). First when I scroll the list it is smooth but when I scroll more to the bottom items the jittering starts.
I observed also that scrolling to the bottom is still smoother than when I scroll back to the begin of the list. Maybe because of the height calculation for the rows above.
But this happens only when the number of items fetched from core data is very high. For only a few hundred items the earthquake approach works pretty well in this messaging app environment.
Note:I used also request.includesPropertyValues = false as recommended by someone in the internet but this could not fix the jittering with large core data fetch results. Also switching from LazyVStack to List was not a solution for the jitter.
I am able to insert, delete or replace items in the macOS menu bar using SwiftUIs CommandGroup command. Is it also possible to rename an existing menu title?
My goal is to replace the 'file' menu title with 'account'.
The rest of the menu bar can stay as it is.
Even when you separate business logic code from your SwiftUI views the previews and also view bodies seems both to count for the unit test code coverage.
I read many articles in the internet about how to test SwiftUI code and archive good code coverage. But they all seem not to be the perfect solution for this task.
Is there any good guide for unit and ui testing SwiftUI code that Apple recommends for their apps?
I have a simple SwiftUI List view with a .toolbar modifier containing three ToolbarItems. The placement is set to bottom.
On the iPhone it seems as if the toolbar is animating from the bottom right corner whenever I launch the app. Having this view as a destination view it looks really strange. The NavigationLink causes the List view to appear from the right while the toolbar slides in from the bottom right corner.
Is there a way to make the bottom toolbar not to slide it from the corner?
I have two Mac Book Pro 16'' 2019. On both I see a very high battery drain using Xcode 13 while using the simulator. Even when the app is not running only the simulator is launched in the background the MacBook gets very hot and the battery drains empty very fast.
For the beta this was ok. But with the RC I expected that this was only a temporary problem. Does some else has this problem also?
On macOS we can add labels directly to SwiftUI slider. Has someone found a way how we can align two or more sliders in a VStack?- label text right aligned- all labels should have the same width (given by the largest text)- all slider bars should have the same but flexible width within the containers frame
Is it possible to use the new http traffic instrument with combines dataTaskPublisher? How can I set a label to the task?
Maybe I misunderstood the the 2019 WWDC video 'Making apps with core data'. Because when I use NSBatchInsertRequest together with NSMergeByPropertyObjectTrumpMergePolicy and unique constraints I can only add new records with the given id. Existing core data objects will not be partially updated.
Example Entity: Person
Attributes:
FirstName = John
LastName = Appleseed
ID = 1234 (unique constraint)
Goal in this example should be to change only the FirstName.
As far as I understood the session video we can omit some attributes and call NSBatchInsertRequest with the above ID and change FirstName to 'Johnny'. But this seems not to work.
Maybe someone has an idea if I am doing something wrong here or if this is really not possible to omit values and change single attribute values.
I want to avoid fetching existing records, updating the values that should be changed and committing the changes back to the persistent store if there is maybe a better solution.
I found this problem many times in the internet but not a solution for this.
I have a SwiftUI list with data fetched from a server. The app uses a periodically background sync (into a core data background context). So the rows of this list can change. To make it simple to understand think of a message app like on the iPhone:
struct MasterView: View {
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Chat.name, ascending: true)],
animation: .none)
private var chats: FetchedResults<Chat>
var body: some View {
List {
ForEach(chats, id: \.chatID) { chat in
NavigationLink(destination: DetailView(chat: chat)) {
Text(chat.name ?? "")
}
}
}
}
}
The detail view is not so important here but keeping with the example of a message app you would have something like the following code to display the messages of a selected chat:
struct DetailView: View {
let chat: Chat
@FetchRequest(
sortDescriptors: [NSSortDescriptor(keyPath: \Message.timestamp, ascending: true)],
animation: .none)
private var messages: FetchedResults<Message>
var body: some View {
List {
ForEach(messages, id: \.messageID) { message in
Text(message.messageText ?? "")
}
}
.onAppear {
if let chatID = chat.chatID {
messages.nsPredicate = NSPredicate(format: "chatID == %@", chatID)
}
}
}
}
In the internet I saw also other examples without using core data for the master view.
If the details view is visible to the user and the master view receives new data from the server the details view will always close (pops) automatically. This happens also when the chatID has not changed and still exists. A change of the master or also if in a split view the master view scrolls outside the visible area then the detail screen will close.
As you see I specified the item ids in both ForEach lists and added also in some further tests identifiers to the NavigationLink or destination of the NavigationLink. But as soon the master list updates the old NavigationLink seems to get deallocated and therefore the detail views will close.
Does someone know how to fix this? Of course I could add a button inside the masters list, store the selection and place the NavigationLink outside from the list item as I saw in one example, but it seems to be more a quick hack as a good solution.
In the latest beta most of the SwiftUI controls have now a labels that are displayed on the left side of the controls. The alignment of the controls and labels works pretty well. Also the TextField is aligned with the other controls. But its title parameter is used as a placeholder inside the TextField while the left side of the TextField is empty.
In my application I have a SwiftUI form with some controls and some number input fields. After the user has entered values the placeholders are not visible and the user cannot see the meaning of each value he entered anymore.
Is there a way that the title of a TextField can be displayed as a label on the left side similar to the other controls instead of a placeholder inside the input field?
Is there a way to add a label to SwiftUI Textfield inside a SwiftUI Form so that the label is aligned with the other controls? Placeholder in textfields will disappear when you enter values so this won't help much.
All other controls have labels in macOS form with the same width so that their controls are correctly aligned. Textfield seems not to have this option. I tried several workaround like HStacks of Text and Textfields or custom alignment guides but without success.
I struggle with the implementation of a compute kernel to solve a relative simple search problem. I want to find the coordinate with the highest pixel value within a MTLTexture.
I have already an implementation where I copy the content of the MTLTexture into CPU memory and apply some min/max commands from the accelerate framework. But maybe there is also a simple solution for this task using Metal which I haven't found yet.
Can someone help me with this hopefully simple problem? I use triple buffering in my metal app similar to Apples Best Practice Guide.
Three threads are simultaneously encoding rendering commands to command buffers enqueued in into one MTLCommandQueue.
What is the best way to debug this scenario with the Xcode's frame debugger? I have some problems setting the capture scopes begin and end point in such a multithreaded environment to isolate the commands encoded from one thread.
Why is there no label for SwiftUI TextField but only a placeholder text? Or is there a modifier that I haven't found yet or a style property? On macOS every control seems to have now a label that is shown on the left side and that is correctly aligned with the other controls in a form. The TextField is also aligned with the other controls but has an empty space left to it. Using the label parameter as a placeholder is not very helpful having multiple textfields and the user entered already some values in them. In this case you cannot see anymore the meaning for all these values. I tried to implement a HStack with a Text and a TextField as a workaround but had trouble to align it with the other controls that have already label parameters in their SwiftUI syntax.